﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Catering_Rules
{
    public class BalanceInfo
    {
        private readonly Catering_Operation.BalanceInfo dal = new Catering_Operation.BalanceInfo();

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Catering_Model.BalanceInfo model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(decimal Balance, decimal Price, decimal PreBalance, decimal PrePrice, int ID)
        {
            return dal.Update(Balance, Price, PreBalance, PrePrice, ID);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.BalanceInfo GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.BalanceInfo GetModel(string strWhere, string orderBy)
        {
            return dal.GetModel(strWhere, orderBy);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, string orderBy)
        {
            return dal.GetList(strWhere, orderBy);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int top, string strWhere, string orderBy)
        {
            return dal.GetList(top, strWhere, orderBy);
        }

        /// <summary>
        /// 获取最新结余信息
        /// </summary>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public DataTable GetLastList(string StrWhere)
        {
            return dal.GetLastList(StrWhere);
        }

        /// <summary>
        /// 获得数量
        /// </summary>
        public int GetCount(string strWhere)
        {
            return dal.GetCount(strWhere);
        }

        public bool AddInDBInfo(int RawID, int ClassID, string InDBId, DateTime AddDate, string StrNumber, string StrPrice, int ChangeID)
        {
            //数据整理
            decimal number = 0;
            decimal price = 0;
            Decimal.TryParse(StrNumber, out number);
            Decimal.TryParse(StrPrice, out price);
            if (number == 0) return false;
            if (price == 0) return false;
            if (AddDate == new DateTime(AddDate.Year, AddDate.Month, AddDate.Day))
            {
                AddDate = new DateTime(AddDate.Year, AddDate.Month, AddDate.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            }

            //第一步：获取品号的库存结余信息
            string StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " AND InDBId='" + InDBId + "' AND AddDate<='" + AddDate.ToString() + "'";
            string StrOrder = " AddDate DESC,ID DESC ";
            DataTable dt = GetList(2, StrWhere, StrOrder).Tables[0];

            //第二步：添加库存结余信息
            Catering_Model.BalanceInfo temp = new Catering_Model.BalanceInfo();
            if (dt == null || (dt != null && dt.Rows.Count == 0))
            {
                temp.RawID = RawID;
                temp.InDBId = InDBId;
                temp.ClassID = ClassID;
                temp.AddDate = AddDate;
                temp.Balance = number;
                temp.Price = price;
                temp.ChangeNumber = number;
                temp.ChangePrice = price;
                temp.ChangeType = 0;
                temp.ChangeID = ChangeID;
                temp.PrePrice = price;
                temp.PreBalance = 0;
                Add(temp);
            }
            else if (dt.Rows.Count > 0)
            {
                decimal prebalance = 0;
                decimal preprice = 0;
                DateTime maxDate = DateTime.MinValue;
                DateTime minDate = DateTime.MinValue;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    decimal tempbalance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                    decimal tempprice = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                    if (i == 0)
                    {
                        prebalance = tempbalance;
                        preprice = tempprice;
                        break;
                        //if (maxDate != AddDate) break;
                    }
                    //if (i == 1)
                    //{
                    //    prebalance = tempbalance;
                    //    preprice = tempprice;
                    //    minDate = tempdate;
                    //    break;
                    //}
                }
                dt.Dispose();

                temp.RawID = RawID;
                temp.InDBId = InDBId;
                temp.ClassID = ClassID;
                temp.ChangeType = 0;
                temp.ChangeNumber = number;
                temp.ChangeID = ChangeID;
                temp.ChangePrice = price;


                //if (minDate == DateTime.MinValue)
                //{
                //    if (maxDate == AddDate)
                //    {
                //        //只有一条数据并且和要插入的时间相同
                //        AddDate = AddDate.AddMinutes(-(new Random().Next(System.Math.Abs(60 - AddDate.Minute))));
                //        prebalance = 0;
                //        preprice = price;
                //    }
                //}
                //else
                //{
                //    //有多条数据并且第一条和要插入的时间相同
                //    AddDate = GetRandomTime(maxDate, new DateTime(maxDate.Year, maxDate.Month, maxDate.Day).AddDays(1));
                //}
                temp.PreBalance = prebalance;
                temp.PrePrice = preprice;
                temp.AddDate = AddDate;
                temp.Balance = prebalance + number;
                if (preprice == price)
                {
                    temp.Price = preprice;
                }
                else
                {
                    temp.Price = (prebalance * preprice + number * price) / (prebalance + number);
                }
                Add(temp);
            }

            //第三步，更改结余信息
            StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " AND InDBId='" + InDBId + "' AND AddDate>'" + AddDate.ToString() + "'";
            StrOrder = "  AddDate ASC,ID ASC ";
            dt = GetList(StrWhere, StrOrder).Tables[0];

            decimal CurrentPrice = temp.Price;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = int.Parse(dt.Rows[i]["ID"].ToString());
                    int changeid = int.Parse(dt.Rows[i]["ChangeID"].ToString());
                    int changetype = int.Parse(dt.Rows[i]["ChangeType"].ToString());
                    string indbid = dt.Rows[i]["InDBId"].ToString();
                    decimal tempbalance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                    decimal tempprice = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                    decimal tempprebalance = Decimal.Parse(dt.Rows[i]["PreBalance"].ToString());
                    decimal temppreprice = Decimal.Parse(dt.Rows[i]["PrePrice"].ToString());
                    decimal tempchangeprice = Decimal.Parse(dt.Rows[i]["ChangePrice"].ToString());
                    decimal tempchangenumber = Decimal.Parse(dt.Rows[i]["ChangeNumber"].ToString());
                    if (changeid == temp.ChangeID && changetype == temp.ChangeType && indbid == temp.InDBId) continue;

                    tempbalance = tempbalance + number;
                    tempprebalance = tempprebalance + number;
                    temppreprice = CurrentPrice;
                    if (temppreprice == tempchangeprice)
                    {
                        tempprice = temppreprice;
                    }
                    else
                    {
                        tempprice = (tempprebalance * temppreprice + tempchangenumber * tempchangeprice) / (tempprebalance + tempchangenumber);
                        CurrentPrice = tempprice;
                    }

                    Update(tempbalance, tempprice, tempprebalance, temppreprice, id);
                }
                dt.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 删除入库时修改结余信息
        /// </summary>
        /// <param name="ChangeID"></param>
        /// <returns>1：已出库，结余不足，删除失败</returns>
        /// <returns>2：操作失误，删除失败</returns>
        /// <returns>3：删除成功</returns>
        /// <returns>6：没有此结余信息</returns>
        public int DelInDBInfo(int ChangeID)
        {
            //第一步：获取品号的库存结余信息
            string StrWhere = " ChangeID=" + ChangeID + " AND ChangeType=0 ";
            string StrOrder = "  AddDate ASC,ID ASC ";
            //要删除的数据
            Catering_Model.BalanceInfo model = GetModel(StrWhere, "");
            if (model != null)
            {
                StrWhere = " ClassID=" + model.ClassID + " AND RawID=" + model.RawID + " AND InDBId='" + model.InDBId + "' AND AddDate>'" + model.AddDate.ToString() + "' AND Balance<" + model.ChangeNumber + " ";
                int count = GetCount(StrWhere);
                if (count > 0) return 1;
                bool flag = Delete(model.ID);
                if (!flag) return 2;
                StrWhere = " ClassID=" + model.ClassID + " AND RawID=" + model.RawID + " AND InDBId='" + model.InDBId + "' AND AddDate>'" + model.AddDate.ToString() + "' ";
                DataTable dt = GetList(StrWhere, StrOrder).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    decimal CurrentPrice = model.PrePrice;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int id = int.Parse(dt.Rows[i]["ID"].ToString());
                        decimal tempbalance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                        decimal tempprice = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                        decimal tempprebalance = Decimal.Parse(dt.Rows[i]["PreBalance"].ToString());
                        decimal temppreprice = Decimal.Parse(dt.Rows[i]["PrePrice"].ToString());
                        decimal tempchangeprice = Decimal.Parse(dt.Rows[i]["ChangePrice"].ToString());
                        decimal tempchangenumber = Decimal.Parse(dt.Rows[i]["ChangeNumber"].ToString());
                        tempbalance = tempbalance - model.ChangeNumber;
                        tempprebalance = tempprebalance - model.ChangeNumber;
                        temppreprice = CurrentPrice;
                        if (temppreprice == tempchangeprice)
                        {
                            tempprice = temppreprice;
                        }
                        else
                        {
                            tempprice = (tempprebalance * temppreprice + tempchangenumber * tempchangeprice) / (tempprebalance + tempchangenumber);
                            CurrentPrice = tempprice;
                        }

                        Update(tempbalance, tempprice, tempprebalance, temppreprice, id);
                    }
                    dt.Dispose();
                }
            }
            return 3;
        }

        /// <summary>
        /// 添加出库
        /// </summary>
        /// <param name="RawID"></param>
        /// <param name="ClassID"></param>
        /// <param name="InDBId"></param>
        /// <param name="AddDate"></param>
        /// <param name="StrNumber"></param>
        /// <param name="StrPrice"></param>
        /// <param name="ChangeID"></param>
        /// <returns>1：结余不足，不能出库</returns>
        /// <returns>4：出库数量不能为0，出库失败</returns>
        /// <returns>2：操作失误，添加失败</returns>
        /// <returns>3：添加成功</returns>
        /// <returns>5：尚未入库，不能出库</returns>
        public int AddFromRecords(int RawID, int ClassID, string InDBId, DateTime AddDate, string StrNumber, string StrPrice, int ChangeID)
        {
            //数据整理
            decimal number = 0;
            decimal price = 0;
            Decimal.TryParse(StrNumber, out number);
            Decimal.TryParse(StrPrice, out price);
            if (number == 0) return 4;

            if (AddDate == new DateTime(AddDate.Year, AddDate.Month, AddDate.Day))
            {
                AddDate = new DateTime(AddDate.Year, AddDate.Month, AddDate.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            }

            //第一步：判断是否可以出库
            string StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " AND InDBId='" + InDBId + "' AND AddDate>'" + AddDate.ToString() + "' AND Balance<" + number + " ";
            int count = GetCount(StrWhere);
            if (count > 0) return 1;

            //第二步：获取品号的库存结余信息
            StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " AND InDBId='" + InDBId + "' AND AddDate<='" + AddDate.ToString() + "'";
            string StrOrder = " AddDate DESC,ID DESC ";
            //Catering_Model.BalanceInfo model = GetModel(StrWhere, StrOrder);
            DataTable dt = GetList(1, StrWhere, StrOrder).Tables[0];
            if (dt == null || (dt != null && dt.Rows.Count == 0)) return 5;

            decimal prebalance = 0;
            decimal preprice = 0;
            DateTime minDate = DateTime.MinValue;
            if (dt.Rows.Count > 0)
            {
                prebalance = Decimal.Parse(dt.Rows[0]["Balance"].ToString());
                preprice = Decimal.Parse(dt.Rows[0]["Price"].ToString());
            }
            dt.Dispose();
            if (prebalance < number) return 1;

            //第三步添加入库
            Catering_Model.BalanceInfo temp = new Catering_Model.BalanceInfo();
            temp.RawID = RawID;
            temp.InDBId = InDBId;
            temp.ClassID = ClassID;
            temp.ChangeType = 1;
            temp.ChangeNumber = number;
            temp.ChangeID = ChangeID;
            temp.ChangePrice = preprice;
            temp.AddDate = AddDate;
            temp.PreBalance = prebalance;
            temp.PrePrice = preprice;
            temp.Balance = prebalance - number;
            temp.Price = preprice;
            bool flag = Add(temp);
            if (!flag) return 2;

            //第四步，更改结余信息
            StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " AND InDBId='" + InDBId + "' AND AddDate>'" + AddDate.ToString() + "'";
            StrOrder = "  AddDate ASC,ID ASC ";
            dt = GetList(StrWhere, StrOrder).Tables[0];

            decimal CurrentPrice = temp.Price;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = int.Parse(dt.Rows[i]["ID"].ToString());
                    decimal tempbalance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                    decimal tempprice = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                    decimal tempprebalance = Decimal.Parse(dt.Rows[i]["PreBalance"].ToString());
                    decimal temppreprice = Decimal.Parse(dt.Rows[i]["PrePrice"].ToString());
                    decimal tempchangeprice = Decimal.Parse(dt.Rows[i]["ChangePrice"].ToString());
                    decimal tempchangenumber = Decimal.Parse(dt.Rows[i]["ChangeNumber"].ToString());
                    tempbalance = tempbalance - number;
                    tempprebalance = tempprebalance - number;
                    temppreprice = CurrentPrice;
                    if (tempbalance < 0) return 1;
                    if (temppreprice == tempchangeprice)
                    {
                        tempprice = temppreprice;
                    }
                    else
                    {
                        tempprice = (tempprebalance * temppreprice + tempchangenumber * tempchangeprice) / (tempprebalance + tempchangenumber);
                        CurrentPrice = tempprice;
                    }

                    Update(tempbalance, tempprice, tempprebalance, temppreprice, id);
                }
                dt.Dispose();
            }
            return 3;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ChangeID"></param>
        /// <returns>2：操作失误，删除失败</returns>
        /// <returns>3：删除成功</returns>
        public int DelFromRecords(int ChangeID)
        {
            //第一步：获取品号的库存结余信息
            string StrWhere = " ChangeID=" + ChangeID + " AND ChangeType=1 ";
            string StrOrder = "  AddDate ASC,ID ASC ";
            //要删除的数据
            Catering_Model.BalanceInfo model = GetModel(StrWhere, "");
            if (model != null)
            {
                bool flag = Delete(model.ID);
                if (!flag) return 2;
                StrWhere = " ClassID=" + model.ClassID + " AND RawID=" + model.RawID + " AND InDBId='" + model.InDBId + "' AND AddDate>='" + model.AddDate.ToString() + "' ";
                DataTable dt = GetList(StrWhere, StrOrder).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    decimal CurrentPrice = model.PrePrice;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int id = int.Parse(dt.Rows[i]["ID"].ToString());
                        decimal tempbalance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                        decimal tempprice = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                        decimal tempprebalance = Decimal.Parse(dt.Rows[i]["PreBalance"].ToString());
                        decimal temppreprice = Decimal.Parse(dt.Rows[i]["PrePrice"].ToString());
                        decimal tempchangeprice = Decimal.Parse(dt.Rows[i]["ChangePrice"].ToString());
                        decimal tempchangenumber = Decimal.Parse(dt.Rows[i]["ChangeNumber"].ToString());
                        DateTime tempadddate = DateTime.Parse(dt.Rows[i]["AddDate"].ToString());
                        if (i == 0 && model.AddDate == tempadddate && tempprebalance != model.Balance) continue;
                        tempbalance = tempbalance + model.ChangeNumber;
                        tempprebalance = tempprebalance + model.ChangeNumber;
                        temppreprice = CurrentPrice;
                        if (temppreprice == tempchangeprice)
                        {
                            tempprice = temppreprice;
                        }
                        else
                        {
                            tempprice = (tempprebalance * temppreprice + tempchangenumber * tempchangeprice) / (tempprebalance + tempchangenumber);
                            CurrentPrice = tempprice;
                        }

                        Update(tempbalance, tempprice, tempprebalance, temppreprice, id);
                    }
                    dt.Dispose();
                }
            }
            return 3;
        }

        public ArrayList GetLastBalanceInfo(string StrClassID, string StrRawID, string InDBId)
        {

            int ClassID = 0;
            int RawID = 0;
            Int32.TryParse(StrClassID, out ClassID);
            Int32.TryParse(StrRawID, out RawID);
            if (ClassID <= 0) return null;
            if (RawID <= 0) return null;
            if (string.IsNullOrEmpty(InDBId)) return null;
            ArrayList al = new ArrayList();
            string StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + "  AND InDBId='" + InDBId + "' ";
            string StrOrder = "  AddDate DESC,ID DESC ";
            Catering_Model.BalanceInfo model = GetModel(StrWhere, StrOrder);
            if (model != null)
            {
                al.Add(model.ChangeID);
                al.Add(model.Balance);
                al.Add(model.Price);
                al.Add(model.ChangeType);
                return al;
            }
            else
            {
                return null;
            }
        }
        public List<ArrayList> GetLastBalanceInfoList(string StrClassID, string StrRawID)
        {
            List<ArrayList> list = new List<ArrayList>();
            int ClassID = 0;
            int RawID = 0;
            Int32.TryParse(StrClassID, out ClassID);
            Int32.TryParse(StrRawID, out RawID);
            if (ClassID <= 0) return null;
            if (RawID <= 0) return null;

            string StrWhere = " ClassID=" + ClassID + " AND RawID=" + RawID + " ";
            DataTable dt = GetLastList(StrWhere);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ArrayList al = new ArrayList();
                    int ChangeID = int.Parse(dt.Rows[i]["ChangeID"].ToString());
                    decimal Balance = Decimal.Parse(dt.Rows[i]["Balance"].ToString());
                    decimal Price = Decimal.Parse(dt.Rows[i]["Price"].ToString());
                    int ChangeType = int.Parse(dt.Rows[i]["ChangeType"].ToString());
                    string indbid = dt.Rows[i]["InDBId"].ToString();
                    al.Add(ChangeID);
                    al.Add(Balance);
                    al.Add(Price);
                    al.Add(ChangeType);
                    al.Add(ChangeType);
                    al.Add(indbid);
                    list.Add(al);
                }
                dt.Dispose();
                return list;
            }
            else
            {
                return null;
            }
        }

        /**/
        /// <summary>
        /// 获取随机时间
        /// <remarks>
        /// 由于Random 以当前系统时间做为种值,所以当快速运行多次该方法所得到的结果可能相同,
        /// 这时,你应该在外部初始化 Random 实例并调用 GetRandomTime(DateTime time1, DateTime time2, Random random)
        /// </remarks>
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public DateTime GetRandomTime(DateTime time1, DateTime time2)
        {
            Random random = new Random();
            return GetRandomTime(time1, time2, random);
        }

        /**/
        /// <summary>
        /// 获取随机时间
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <param name="random"></param>
        /// <returns></returns>
        public DateTime GetRandomTime(DateTime time1, DateTime time2, Random random)
        {
            DateTime minTime = new DateTime();
            DateTime maxTime = new DateTime();

            System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);

            // 获取两个时间相隔的秒数
            double dTotalSecontds = ts.TotalSeconds;
            int iTotalSecontds = 0;

            if (dTotalSecontds > System.Int32.MaxValue)
            {
                iTotalSecontds = System.Int32.MaxValue;
            }
            else if (dTotalSecontds < System.Int32.MinValue)
            {
                iTotalSecontds = System.Int32.MinValue;
            }
            else
            {
                iTotalSecontds = (int)dTotalSecontds;
            }


            if (iTotalSecontds > 0)
            {
                minTime = time2;
                maxTime = time1;
            }
            else if (iTotalSecontds < 0)
            {
                minTime = time1;
                maxTime = time2;
            }
            else
            {
                return time1;
            }

            int maxValue = iTotalSecontds;

            if (iTotalSecontds <= System.Int32.MinValue)
                maxValue = System.Int32.MinValue + 1;

            int i = random.Next(System.Math.Abs(maxValue));

            return minTime.AddSeconds(i);
        }

        public decimal GetBalance(int ClassID, string StrInDBID, DateTime AddDate)
        {
            decimal result = 0;
            decimal result1 = 0;
            result = dal.GetBalance(ClassID, StrInDBID, AddDate);
            string strwhere = " ClassID=" + ClassID + " AND InDBid='" + StrInDBID + "' AND AddDate<= '" + AddDate + "' ";
            Catering_Model.BalanceInfo model = new Catering_Rules.BalanceInfo().GetModel(strwhere, " AddDate DESC,ID DESC ");
            if (model != null)
            {
                result1 = model.Balance;
                if (result < 0)
                {
                    return result1;
                }
                else
                {
                    return result < result1 ? result : result1;
                }
            }
            else
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 获取原料结余（此原料的全部品号的总结余）
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="StrRawID"></param>
        /// <param name="AddDate"></param>
        /// <returns></returns>
        public decimal GetBalanceByRawID(int ClassID, string StrRawID, DateTime AddDate)
        {
            string strwhere = " ClassID=" + ClassID + " AND RawID='" + StrRawID + "' AND AddDate<= '" + AddDate + "' ";
            DataTable dt = dal.GetLastList(strwhere);
            ArrayList al = new ArrayList();
            decimal result = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string indbid = dt.Rows[i]["InDBID"].ToString();
                    if (al.Contains(indbid)) continue;
                    decimal balance = Convert.ToDecimal(dt.Rows[i]["Balance"]);
                    result += balance;
                    al.Add(indbid);
                }
                dt.Dispose();
            }
            return result;
        }

        /// <summary>
        /// 获取原料单价（此原料的全部品号的平均价格）
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="StrRawID"></param>
        /// <param name="AddDate"></param>
        /// <returns></returns>
        public decimal GetPriceByRawID(int ClassID, string StrRawID, DateTime AddDate)
        {
            string strwhere = " ClassID=" + ClassID + " AND RawID='" + StrRawID + "' AND AddDate<= '" + AddDate + "' ";
            DataTable dt = dal.GetLastList(strwhere);
            ArrayList al = new ArrayList();
            decimal result = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string indbid = dt.Rows[i]["InDBID"].ToString();
                    if (al.Contains(indbid)) continue;
                    decimal price = Convert.ToDecimal(dt.Rows[i]["Price"]);
                    result += price;
                    al.Add(indbid);
                }
                dt.Dispose();
            }
            if (al.Count > 0) result = result / al.Count;
            return result;
        }
    }
}
